package czk;

/**
 * 死锁
 */
public class DeadLock {
    public static void main(String[] args) {
        new Thread(new A(),"thread-a").start();
        new Thread(new B(),"thread-b").start();
    }
}
class B implements Runnable{
    @Override
    public void run() {
        while (true) {
            synchronized (A.class) {
                System.out.println("AAA");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (B.class) {
                    System.out.println("BBB");
                }
            }
        }
    }
}
class A implements Runnable{
    @Override
    public void run() {
        while (true) {
            synchronized (B.class) {
                System.out.println("AAA");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (A.class) {
                    System.out.println("BBB");
                }
            }
        }
    }
}

